package com.vates.vsp.practico.alumnos.business;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.vates.vsp.practico.alumnos.service.AlumnosService;
import com.vates.vsp.practico.alumnos.view.AlumnosView;
import com.vates.vsp.practico.commons.ListDataModel;
import com.vates.vsp.practico.commons.UseCaseMode;

/**
 * Capa Negocio
 */
@ManagedBean(name = "alumnosListener")
@SessionScoped
public class AlumnosListener {

	@ManagedProperty(value = "#{alumnosView}")
	private AlumnosView alumnosView;
	@ManagedProperty(value = "#{alumnosService}")
	private AlumnosService alumnosService;

	public AlumnosView getAlumnosView() {
		return alumnosView;
	}

	public void setAlumnosView(AlumnosView alumnosView) {
		this.alumnosView = alumnosView;
	}

	public AlumnosService getAlumnosService() {
		return alumnosService;
	}

	public void setAlumnosService(AlumnosService alumnosService) {
		this.alumnosService = alumnosService;

	}

	public String mostrarAlumnos() {
		alumnosView.setUseCaseMode(UseCaseMode.CONSULTA);
		List<Alumnos> resultado = alumnosService.getAllAlumnos();
		alumnosView.setAlumnos(new ListDataModel<Alumnos>(resultado));
		return "listadoAlumnos";
	}

	public String mostrarAlumno() {
		alumnosView.setUseCaseMode(UseCaseMode.CONSULTA);
		alumnosView.setAlumno(alumnosView.getAlumnos().getRowData());
		return "alumno";
	}

	public String cargarNuevoAlumno() {
		alumnosView.setUseCaseMode(UseCaseMode.NUEVO);
		alumnosView.setAlumno(new Alumnos());
		return "nuevoAlumno";
	}

	public String eliminarAlumno() {
		alumnosView.setUseCaseMode(UseCaseMode.ELIMINACION);
        alumnosView.setAlumno(alumnosView.getAlumnos().getRowData());
		return "eliminarAlumno";

	}

	public String editarAlumno() {
		alumnosView.setUseCaseMode(UseCaseMode.MODIFICACION);
        alumnosView.setAlumno(alumnosView.getAlumnos().getRowData());
        return "editarAlumno";
	}
	
	public String guardar(){
		alumnosService.guardarAlumno(alumnosView.getAlumno());
		return mostrarAlumnos();
	}
	
	public String eliminar(){
		alumnosService.eliminarAlumno(alumnosView.getAlumno());
		return mostrarAlumnos();
	}

	public String volver() {
		return "volver";
	}

	public String salir() {
		return "salir";
	}
}
